<?php
/*
点喜欢收藏
*/
class ShareFavAction extends UserAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	public function execute()
	{
		
		$this->code 	= 0;
		$share_id 		= intval($this->context->post('share_id'));
		if (!$share_id > 0){
			$this->code 	= 105;//不存在
			return;
		}
		
		
		$shareVtui = true;//在此验证是否有这个分享
		
		if (!$shareVtui){
			$this->code 	= 105;//不存在
			return;
		}
		
		if ($shareVtui['user_id'] == $this->data['user']['id']){
			$this->code 	= 101;//自己的
			return;
		}
		
		
		
		$collectService	= new BaseDbService('collect');
		$tmp	= $collectService->count(array('share_id'=>'eq:'.$share_id, 'user_id'=>'eq:'.$this->data['user']['id']));
		if ($tmp){
			$this->code 	= 100;//已经喜欢过了
			$map = array();
		}else{
			$map = array('share_id'=>$share_id, 'user_id'=>$this->data['user']['id']);
			$id	= $collectService->add($map);
			if ($id){
				//更新分享表
				$shareService	= new BaseDbService('share');
				$shareInfo		= $shareService->get($share_id);
				if ($shareInfo){
					$shareService->update($share_id, array('collect_num'=>'expression:collect_num+1'));
					$collect_num = $shareInfo['collect_num']+1;
				}else{
					$shareService->add(array('id'=>$share_id, 'collect_num'=>1));
					$collect_num = 1;
				}
				$this->data['share']['collect_num'] = $collect_num;
				$this->code 	= 102;//成功
				//$this->message	= '谢谢您的喜欢';
				
				//TODO 更新服务器被喜欢数量
			}else{
				$this->code 	= 103;//失败
				//$this->message	= '哎呀，不小心被雷劈了下真倒霉，再试一下';
			}
		}
	}
}
